package cn.quevo.cube.api.define;

import cn.quevo.common.annotation.RequiresPermissions;
import cn.quevo.common.entity.PageResult;
import cn.quevo.common.entity.ResultEntity;
import cn.quevo.common.enums.ResultStatusEnum;
import cn.quevo.common.lang.StringUtils;
import cn.quevo.core.spring.SpringController;
import cn.quevo.cube.provide.define.entity.CubeModel;
import cn.quevo.cube.provide.define.entity.query.CubeModelQuery;
import cn.quevo.cube.provide.define.service.CubeModelService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 模型 RestController
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/12/11 14:57:09
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
@RestController("cubeModelController")
@RequestMapping("/cube/")
public class CubeModelRestController extends SpringController{

    @Resource(name="cubeModelService")
    private CubeModelService cubeModelService;

    /**
     * 分页加载模型
     *
     * @param cubeModelQuery
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/11 14:57:09
     */
    @RequestMapping(value="cubeModel",method= RequestMethod.GET)
    @RequiresPermissions("cube:cubeModel:list")
    public ResultEntity page(CubeModelQuery cubeModelQuery){
        if (StringUtils.isBlank(cubeModelQuery.getCubeDbId())) {
            return PageResult.success();
        }
        cubeModelQuery.setIsPaging(false);
        return cubeModelService.getPage(cubeModelQuery);
    }

    /**
     * 保存模型
     *
     * @param cubeModel
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/11 14:57:09
     */
    @RequestMapping(value="cubeModel",method=RequestMethod.POST)
    @RequiresPermissions("cube:cubeModel:create")
    public ResultEntity create(CubeModel cubeModel){
        if(cubeModelService.create(cubeModel) <= 0){
            return new ResultEntity(ResultStatusEnum.RECORD_REPEAT);
        }
        return ResultEntity.success();
    }

    /**
     * 修改模型
     *
     * @param cubeModel
     * @param id
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/11 14:57:09
     */
    @RequestMapping(value="cubeModel//{id:\\d+}",method=RequestMethod.PUT)
    @RequiresPermissions("cube:cubeModel:update")
    public ResultEntity update(CubeModel cubeModel, @PathVariable("id") Long id){
        if (StringUtils.isBlank(id)) {
            return ResultEntity.fail(ResultStatusEnum.RECORD_UNSELECTED);
        }
        cubeModel.setId(id);
        if (StringUtils.isBlank(this.cubeModelService.update(cubeModel))) {
            return ResultEntity.fail(ResultStatusEnum.RECORD_REPEAT);
        }
        return ResultEntity.success();
    }
}
